{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Matrix Similarity After Cyclic Shifts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math #matrix #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学 #矩阵 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: areSimilar"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #循环移位后的矩阵相似检查"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个<strong>下标从 0 开始</strong>且大小为 <code>m x n</code> 的整数矩阵 <code>mat</code> 和一个整数 <code>k</code> 。请你将矩阵中的<strong> 奇数</strong> 行循环 <strong>右</strong> 移 <code>k</code> 次，<strong>偶数</strong> 行循环 <strong>左</strong> 移 <code>k</code> 次。</p>\n",
    "\n",
    "<p>如果初始矩阵和最终矩阵完全相同，则返回 <code>true</code> ，否则返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong class=\"example\">示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2023/10/29/similarmatrix.png\" style=\"width: 500px; height: 117px;\" />\n",
    "\n",
    "初始矩阵如图一所示。\n",
    "图二表示对奇数行右移一次且对偶数行左移一次后的矩阵状态。\n",
    "图三是经过两次循环移位后的最终矩阵状态，与初始矩阵相同。\n",
    "因此，返回 true 。\n",
    "</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[2,2],[2,2]], k = 3\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>由于矩阵中的所有值都相等，即使进行循环移位，矩阵仍然保持不变。因此，返回 true 。\n",
    "</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[1,2]], k = 1\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>循环移位一次后，mat = [[2,1]]，与初始矩阵不相等。因此，返回 false 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= mat.length &lt;= 25</code></li>\n",
    "\t<li><code>1 &lt;= mat[i].length &lt;= 25</code></li>\n",
    "\t<li><code>1 &lt;= mat[i][j] &lt;= 25</code></li>\n",
    "\t<li><code>1 &lt;= k &lt;= 50</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [matrix-similarity-after-cyclic-shifts](https://leetcode.cn/problems/matrix-similarity-after-cyclic-shifts/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [matrix-similarity-after-cyclic-shifts](https://leetcode.cn/problems/matrix-similarity-after-cyclic-shifts/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,2,1,2],[5,5,5,5],[6,3,6,3]]\\n2', '[[2,2],[2,2]]\\n3', '[[1,2]]\\n1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areSimilar(self, A: List[List[int]], k: int) -> bool:\n",
    "        for a in A:\n",
    "            n = len(a)\n",
    "            for i in range(n):\n",
    "                if a[i]!=a[(i+k)%n]:\n",
    "                    return False\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areSimilar(self, mat: List[List[int]], k: int) -> bool:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        k %= n\n",
    "        mat2 = [[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            if i % 2 == 0:\n",
    "                mat2[i] = mat[i][k:] + mat[i][:k]\n",
    "            else:\n",
    "                mat2[i] = mat[i][-k:] + mat[i][:-k]\n",
    "        return mat2 == mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areSimilar(self, mat: List[List[int]], k: int) -> bool:\n",
    "        k%=len(mat[0])\n",
    "        x=[]\n",
    "        def f(a,k):\n",
    "            return a[-k:]+a[:-k]\n",
    "        def f2(a,k):\n",
    "            return a[k:]+a[:k]\n",
    "        \n",
    "        for i,v in enumerate(mat):\n",
    "            t=[]\n",
    "            if i%2==1:\n",
    "                t=f(v,k)\n",
    "            else:\n",
    "                t=f(v,k)\n",
    "            if t!=v:\n",
    "                return False\n",
    "        return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areSimilar(self, mat: List[List[int]], k: int) -> bool:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        k = k % n\n",
    "        if k == 0:\n",
    "            return True\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if mat[i][j] != mat[i][(j + k) % n]:\n",
    "                    return False\n",
    "        return True\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
